package com.gdth.widget;

import com.gdth.R;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.WindowManager;
import android.widget.ImageView;

/** 
 * @author Arvin: 
 * @version 2015年6月10日 上午11:19:07 
 * 
 */
public class SplashActivity extends Activity {
	/**
	 * 启动界面挑战时间
	 */
	public int DelayedTime = 100, BootTime = 2000;
	
	public ImageView mImageView;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		//setContentView(R.layout.system_splash);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,    
        WindowManager.LayoutParams.FLAG_FULLSCREEN); 
		
	}
	
	protected void initView(boolean isLanch, String MainName, String BootName, int imgId){
		
//		mImageView = (ImageView) findViewById(R.id.splash_img);
		mImageView.setImageResource(imgId);
		
		if (isLanch){
			postDelayed(DelayedTime, MainName);
		}
		else {
			//读取SharedPreferences中需要的数据
			SharedPreferences preferences = getSharedPreferences("count", MODE_PRIVATE);
	        int count = preferences.getInt("count", 0);
	 
	        //判断程序与第几次运行，如果是第一次运行则跳转到引导页面
	        if (count == 0) {
	        	joinToActivity(BootName);
	        	
	            Editor editor = preferences.edit();
	            
	            //存入数据
	            editor.putInt("count", ++count);
	            //提交修改
	            editor.commit();
	            
	            }
	        else{
	        	Editor editor = preferences.edit();
	        	 
	            //存入数据
	            editor.putInt("count", ++count);
	            //提交修改
	            editor.commit();

		        postDelayed(BootTime, MainName);
	        }
        }
	}
	
	/**
	 * 延时加载
	 * @param time
	 * @param ActivityName
	 */
	public void postDelayed(int time, final String ActivityName){
		try{
			new Handler().postDelayed(new Runnable() {
	            public void run() {
	            	joinToActivity(ActivityName);
	            }
	        }, time);
		}
		catch(Exception e){
			//Log.w("", e.getMessage());
		}
	}
	
	/**
	 * 跳转页面
	 * @param AcitivityName
	 */
	public void joinToActivity(String AcitivityName){
		Intent mIntent = new Intent();
		mIntent.setClassName(this, AcitivityName);
		
		startActivity(mIntent);
		
		finish();
	}
}
